// pages/admin4TeacherPage/classQRcode/classQRcode.js
var app = getApp();
import util from '../../utils/util.js';
var p = 1;
import homeworkAPI from '../../api/homeworkAPI'
import commonAPI from '../../api/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    homeworkInfo: {},
    tabs: [],
    priTmplIdArr:[],
    activeTab: 0,
    options: '',
    repeatedData: [],
    norepeatData: [],
    tableHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getSubscribeMsg();// 订阅消息
    that.setData({
      options: options
    });
    const titles = ['未跟读', '已跟读']
    const tabs = titles.map(item => ({ title: item }))
    this.setData({ tabs })
    
    app.getappsysteminfo(function (res) { // 设置app屏幕的高度
      that.setData({
        tableHeight: res.windowHeight
      });
    });
  },

    
// 获取订阅消息
getSubscribeMsg: function () {
  var that = this;
  commonAPI.getSubscribeMsg({ pageUrl: util.getCurrentPageUrl() }).then(res => {
    if (res.code == '200' && res.data.length > 0) {
      var arr = res.data[0].priTmplIdArr.split(",")
      that.setData({
        priTmplIdArr: arr
      })
    }
  })
},

  initData: function () {
    var that = this;
    homeworkAPI.getRepeatStatusdetail({homeworkId: that.data.options.hwid}).then(res => {
    if (res.code == '200') {
      that.setData({
        repeatedData: res.data.repeatedHwList,
        norepeatData: res.data.norepeatlist,
        homeworkInfo: res.data.hwsaifnolist.length > 0 ? res.data.hwsaifnolist[0] : null
      })
    }
  })
  },

  onTabCLick(e) {
    const index = e.detail.index
    this.setData({ activeTab: index })
  },

  onChange(e) {
    const index = e.detail.index
    this.setData({ activeTab: index })
  },

  // 学员回复提醒
  subscribeMessage: function (e) {
    var that = this;
    wx.requestSubscribeMessage({
      tmplIds: that.data.priTmplIdArr,
      success(res) {}
    })
    var voiceObj = that.data.norepeatData[e.currentTarget.dataset.index];
    var voiceArr = new Array();
    voiceArr.push(voiceObj);
    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', { hmdVoiceArr: voiceArr,'isMember':app.isMember})
      }
    }) 
  },

  // 再次跟读
  dorepeatagain: function (e) {
    var that = this;
    wx.requestSubscribeMessage({
      tmplIds: that.data.priTmplIdArr,
      success(res) {}
    })
    var voiceObj = that.data.norepeatData[e.currentTarget.dataset.index];
    var voiceArr = new Array();
    voiceArr.push(voiceObj);
    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', { hmdVoiceArr: voiceArr,'isMember':app.isMember})
      }
    })
  },

  // 去上传
  uploadFilter: function () {
    var that = this;
    wx.navigateTo({
      url: '../uploadImgorVideo/uploadImgorVideo',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(res) {
          // that.setData({
          //   classItem:res.classItem
          // })
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { homeworkInfo: that.data.homeworkInfo})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();// 作业信息
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var t = JSON.stringify(that.data.options.hwid);
    let title, imageUrl = 'https://leshijiewx.com/WendaVoice/upload/wx2017/wxsmallprogram/imgs/homework.png';
    if (res.from === 'button') { // 来自页面内转发按钮
      title = that.data.homeworkInfo.wdTeacherName + '布置作业：' + that.data.homeworkInfo.homeworkTitle;
    }
    if (res.from === 'menu') {
      title = that.data.homeworkInfo.wdTeacherName + '布置作业：' + that.data.homeworkInfo.homeworkTitle;
    }
    return {
      title: title,
      imageUrl: imageUrl, //这个是分享的图片
      path: 'pages/index/index',
    }

  }

})